package com.itheima.homework4;

public class Box {
    private int milk;
    private boolean flag = false;

    public synchronized void storeMilk(int milk) {
        if (flag) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.milk = milk;
        System.out.println("送奶工将第" + this.milk + "瓶奶放入奶箱");
        flag = true;
        notifyAll();
    }


    public synchronized void getMilk() {
        if (!flag) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("用户拿到第" + this.milk + "瓶奶");
        flag = false;
        notifyAll();
    }
}

